package com.un.ebs.businessObject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.un.ebs.core.enums.DataTypeEnum;
import com.un.ebs.core.enums.QueryTypeEnum;
import com.un.ebs.core.enums.RequestArgEnum;
import com.un.ebs.core.metadata.FunctionInfo;
import com.un.ebs.core.metadata.MetadataField;
import com.un.ebs.core.metadata.RequestArg;
import com.un.ebs.sys.domain.Column;
import com.un.ebs.sys.domain.Table;
import com.un.ebs.sys.enums.DisplayEnums;
import com.un.ebs.sys.enums.FunctionsEnums;
import com.un.ebs.sys.mapper.ColumnMapper;
import com.un.ebs.businessObject.service.RelListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zc
 * @ClassName RelListServiceImpl
 * @Description RelListServiceImpl
 * @Date 2021/1/12
 */
@Service
@Slf4j
public class RelListServiceImpl implements RelListService {
	@Autowired
	private ColumnMapper columnMapper;

	/**
	 * 获取查询函数
	 *
	 * @return
	 */
	@Override
	public FunctionInfo getListFunc(Table table) {
		FunctionInfo func = new FunctionInfo();
		String url = FunctionsEnums.LIST.getUrl();
		String desc = FunctionsEnums.LIST.getDesc();
		List<RequestMethod> method = Arrays.asList(FunctionsEnums.LIST.getRequestMethod());

		func.setUrl(String.format(url, table.getId()));
		func.setDescription(desc);
		func.setMethod(method);
		func.setName(table.getTableName());

		List<RequestArg> requestArgs = new ArrayList<>();
		List<MetadataField> metadataFields = new ArrayList<>();
		LambdaQueryWrapper<Column> qw = new LambdaQueryWrapper<Column>();
		qw.eq(Column::getTableId, table.getId());
		List<Column> columns = columnMapper.selectList(qw);
		columns = columns.stream().filter(x -> !DisplayEnums.getColumnNameList().contains(x.getColumnName()))
				.collect(Collectors.toList()); // 过滤支持相关字段
		for (Column column : columns) {
			if( column.getQueryType()==QueryTypeEnum.LikeQuery || column.getQueryType()==QueryTypeEnum.QuickQuery || column.getQueryType()==QueryTypeEnum.AdvancedQuery){
				RequestArg requestArg = new RequestArg();
				requestArg.setName(column.getColumnName());
				requestArg.setDisplayName(column.getColumnDesc());
				try {
					requestArg.setDataType(column.getColumnType());
				} catch (Exception e) {
					e.printStackTrace();
					log.info(column.getId());
				}
				requestArg.setIsArray(0);
				requestArg.setRequestType(RequestArgEnum.RequestBody);
				requestArgs.add(requestArg);
			}

			MetadataField metadataField = new MetadataField();
			metadataField.setName(column.getColumnName());
			metadataField.setDisplayName(column.getColumnDesc());
			metadataField.setDataType(column.getColumnType());
			metadataField.setIsArray(0);
			metadataFields.add(metadataField);
		}

		List<RequestArg> request = getListRequest(requestArgs);
		List<MetadataField> response = getListResponse(metadataFields);
		func.setRequest(request);
		func.setResponse(response);
		return func;
	}

	/**
	 * 构造返回体
	 *
	 * @param metadataFields record 内的字段描述
	 * @return
	 */
	private List<MetadataField> getListResponse(List<MetadataField> metadataFields) {
		// 1.构造code
		MetadataField m1 = new MetadataField();
		m1.setName("code");
		m1.setIsArray(0);
		m1.setDataType(DataTypeEnum.INT);
		// 2.构造message
		MetadataField m2 = new MetadataField();
		m2.setName("message");
		m2.setIsArray(0);
		m2.setDataType(DataTypeEnum.STRING);
		// 3.构造data
		MetadataField m3 = new MetadataField();
		m3.setName("data");
		m3.setIsArray(0);
		m3.setDataType(DataTypeEnum.OBJECT);
		m3.setChildField(metadataFields);
		// 4.构造data_children
		List<MetadataField> list = new ArrayList<>();

		list.add(m1);
		list.add(m2);
		list.add(m3);
		return list;
	}

	/**
	 * 构造请求体
	 *
	 * @param requestArgs 请求字段
	 * @return
	 */
	private List<RequestArg> getListRequest(List<RequestArg> requestArgs) {
		RequestArg r1 = new RequestArg();
		r1.setName("sortName");
		r1.setDisplayName("自定义排序字段");
		r1.setDataType(DataTypeEnum.STRING);
		r1.setRequestType(RequestArgEnum.RequestParam);
		r1.setIsArray(0);
		requestArgs.add(r1);
		return requestArgs;
	}
}
